﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace OpenScrum.db.IntegrationTests
{
    [TestFixture]
    public class ProjectTests
    {

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            AddTestData();
        }        

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            RemoveTestData();
        }

        [Test]
        public void Read_Existing_Project()
        {        
            Project project = null;
            using(var context = new OpenScrumDataContext())
            {
                project = context.Projects.SingleOrDefault(p => p.Name == "Open Scrum");
            }
            Assert.NotNull(project);
        }        

        [Test]
        public void OpenScrum_Project_Contains_4_TeamMembers()
        {            
            int teamMemberCount = 0;
            using(var context = new OpenScrumDataContext())
            {
                teamMemberCount = context.Projects.SingleOrDefault(p => p.Name == "Open Scrum").TeamMembers.Count;
            }
            Assert.AreEqual(4, teamMemberCount);
        }

        [Test]
        public void There_Are_4_TeamMembers()
        {
            int teamMemberCount = 0;
            using (var context = new OpenScrumDataContext())
            {
                teamMemberCount = context.TeamMembers.Count();
            }
            Assert.AreEqual(4, teamMemberCount);
        }
        
        private void RemoveTestData()
        {
            using(var context = new OpenScrumDataContext())
            {
                context.BacklogItems.ToList().ForEach(bli => context.BacklogItems.Remove(bli));
                context.Projects.ToList().ForEach(p => context.Projects.Remove(p));
                context.TeamMembers.ToList().ForEach(t => context.TeamMembers.Remove(t));
                context.SaveChanges();
            }                
        }

        
        private void AddTestData()
        {
            
            using (var context = new OpenScrumDataContext())
            {
                // create projects
                var openScrum = new Project { Name = "Open Scrum" };

                // create team members
                var brett = new TeamMember { Name = "Brett" };
                var byron = new TeamMember { Name = "Byron" };
                var bryan = new TeamMember { Name = "Bryan" };
                var cory = new TeamMember { Name = "Cory" };

                openScrum.TeamMembers = new List<TeamMember>();

                openScrum.TeamMembers.Add(brett);
                openScrum.TeamMembers.Add(byron);
                openScrum.TeamMembers.Add(bryan);
                openScrum.TeamMembers.Add(cory);

                // create backlog items
                var bli1 = new BacklogItem
                               {
                                   Name = "Create login screen",
                                   Description = "As a user I can login to Open Scrum.",
                                   CreatedBy = brett,
                                   Project = openScrum
                               };

                openScrum.BacklogItems = new List<BacklogItem>();
                openScrum.BacklogItems.Add(bli1);

                context.Projects.Add(openScrum);
                context.SaveChanges();
            }
        }                
    }
}
